﻿using InventoryIssue.API.Write.Application.Command.Reportloss;
using InventoryIssue.Domain;
using InventoryIssue.ErrorCode;
using InventoryIssue.Infrastructrue.Implement;
using MediatR;
using MySqlX.XDevAPI.Common;

namespace InventoryIssue.API.Write.Application.Handler.Reportloss
{
    public class DelReportlossHandler : IRequestHandler<DelReportlossCommand, APIResult<int>>
    {
        private readonly DatabaseContext db;

        public DelReportlossHandler(DatabaseContext db)
        {
            this.db = db;
        }
        public async Task<APIResult<int>> Handle(DelReportlossCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = await db.SqlSugar.Queryable<ReportlossModel>()
                .Where(x => x.Id == request.Id)
                .FirstAsync();
            res.IsDel = true;
            int mainUpdateCount = await db.SqlSugar.Updateable(res)
            .WhereColumns(x => x.Id)
            .ExecuteCommandAsync();
            if (mainUpdateCount > 0)
            {
                result.Message = "删除成功";
                result.Code = APIEnum.Ok;
            }
            else
            {
                result.Message = "删除失败（主表或关联表无更新）";
                result.Code = APIEnum.Fail;
            }
            return result;
        }
    }
}
